/*
 * @lc app=leetcode.cn id=1122 lang=javascript
 *
 * [1122] 数组的相对排序
 */

// @lc code=start
/**
 * @param {number[]} arr1
 * @param {number[]} arr2
 * @return {number[]}
 */
var relativeSortArray = function (arr1, arr2) {
  return arr1
    .map(item => ({
      val: item,
      index: arr2.indexOf(item)
    }))
    .sort((a, b) => {
      if (a.index === -1 && b.index === -1) {
        return a.val - b.val;
      }
      if (a.index === -1 && b.index !== -1) {
        return 1;
      }
      if (a.index !== -1 && b.index === -1) {
        return -1;
      }
      return a.index - b.index;
    })
    .map(item => item.val);
};
// @lc code=end

